package cn.tedu._05mvcboot03.controller;

import cn.tedu._05mvcboot03.common.response.JsonResult;
import cn.tedu._05mvcboot03.common.response.StatuCode;
import cn.tedu._05mvcboot03.mapper.UserMapper;
import cn.tedu._05mvcboot03.pojo.dto.UserArticleDTO;
import cn.tedu._05mvcboot03.pojo.dto.UserLoginDTO;
import cn.tedu._05mvcboot03.pojo.entity.MvcArticle;
import cn.tedu._05mvcboot03.pojo.vo.ArticleVO;
import cn.tedu._05mvcboot03.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("v1/users/")
@Slf4j
@Api(tags = "用户模块")
@Validated
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("login")
    @ApiOperation("登录功能")
    public JsonResult login(@RequestBody UserLoginDTO userLoginDTO, @ApiIgnore HttpSession session) {
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();
        UserVO userVO = userMapper.selectUser(username, password);
        log.error("userVO:" + userVO);
        if (userVO != null) {
            if (userVO.getPassword().equals(userLoginDTO.getPassword())) {
                session.setAttribute("user", userVO);
                return JsonResult.ok();
            }
            return new JsonResult(StatuCode.USERNAME_PASSWORD_ERROR);
        }
        return new JsonResult(StatuCode.USERNAME_PASSWORD_ERROR);
    }

    @PostMapping("article")
    @ApiOperation("发表文章功能")
    public JsonResult insertArticle(@RequestBody @Validated UserArticleDTO userArticleDTO, @ApiIgnore HttpSession session) {
        UserVO userVO = (UserVO) session.getAttribute("user");
        log.error("userVO:" + userVO);
        if (userVO != null) {
            if (userArticleDTO.getTitle().isEmpty() || userArticleDTO.getContent().isEmpty()) {
                return new JsonResult(StatuCode.VALIDATED_ERROR, "标题或者内容不能为空... ... ");
            }
            MvcArticle mvcArticle = new MvcArticle();
            BeanUtils.copyProperties(userArticleDTO, mvcArticle);
            mvcArticle.setUserId(userVO.getId());
            userMapper.insertArticle(mvcArticle);
            return JsonResult.ok();
        }
        return new JsonResult(StatuCode.NOT_LOGIN);
    }

    @GetMapping("selectByArticleId")
    @ApiOperation("查询指定文章功能")
    public JsonResult selectByArticleId(@Range(min = 1, max = 100, message = "文章id必须在1到100之间") @RequestParam("id") Long id) {
        ArticleVO articleVO = userMapper.selectByArticleId(id);
        if (id < 1 || id > 100) {
            log.error("id:" + id);
            return new JsonResult(StatuCode.VALIDATED_ERROR, articleVO);
        }
        return JsonResult.ok(articleVO);
    }
}
